package com.mlethe.android.demo.wheelview;

import android.os.Bundle;

import com.mlethe.android.demo.R;
import com.mlethe.library.dialog.DialogFragment;
import com.mlethe.library.widget.wheelview.WheelView;

import java.util.ArrayList;
import java.util.List;

public class AgeChooseDialog extends DialogFragment {

    private WheelView<String> ageWv;
    private OnConfirmListener<String> mListener;

    public AgeChooseDialog setOnConfirmListener(OnConfirmListener<String> listener) {
        this.mListener = listener;
        return this;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_custom_age;
    }

    @Override
    protected void onBindViewHolder(Bundle savedInstanceState, ViewHolder holder) {
        fullWidth().formBottom(true)
                .setOnDismissListener(dialogInterface -> {
                    ageWv = null;
                    mListener = null;
                });
        holder.setOnClickListener(R.id.closeTv, view -> dismiss())
                .setOnClickListener(R.id.confirmTv, view -> {
                    dismiss();
                    if (ageWv != null && mListener != null) {
                        mListener.onClick(ageWv.getSelectedItemData());
                    }
                });
        ageWv = holder.getView(R.id.ageWv);
        List<String> list = new ArrayList<>(1);
        for (int i = 0; i < 20; i++) {
            list.add(i + "日");
        }
        ageWv.setData(list);
        // 经过测试 OGG 格式比 MP3 效果好
        ageWv.setSoundEffectResource(R.raw.button_choose);
        ageWv.setSoundEffect(true);
    }

    public interface OnConfirmListener<T> {
        void onClick(T item);
    }
}
